package com.iknow.task;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import com.iknow.util.Loger;
import java.util.Observable;
import java.util.Observer;

public abstract class GenericTask extends AsyncTask<TaskParams, Object, TaskResult>
  implements Observer
{
  private static final String TAG = "GenericTask";
  private boolean isCancelable;
  private TaskListener mListener = null;

  public GenericTask()
  {
    this.isCancelable = true;
  }

  protected abstract TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams);

  protected TaskResult doInBackground(TaskParams[] paramArrayOfTaskParams)
  {
    return _doInBackground(paramArrayOfTaskParams);
  }

  public void doPublishProgress(Object[] paramArrayOfObject)
  {
    super.publishProgress(paramArrayOfObject);
  }

  public TaskListener getListener()
  {
    return this.mListener;
  }

  protected void onCancelled()
  {
    super.onCancelled();
    if (this.mListener != null)
      this.mListener.onCancelled(this);
    Loger.i("GenericTask", this.mListener.getName() + " has been Cancelled.");
  }

  protected void onPostExecute(TaskResult paramTaskResult)
  {
    super.onPostExecute(paramTaskResult);
    if (this.mListener != null)
      this.mListener.onPostExecute(this, paramTaskResult);
  }

  protected void onPreExecute()
  {
    super.onPreExecute();
    if (this.mListener != null)
      this.mListener.onPreExecute(this);
  }

  protected void onProgressUpdate(Object[] paramArrayOfObject)
  {
    super.onProgressUpdate(paramArrayOfObject);
    if ((this.mListener != null) && (paramArrayOfObject != null) && (paramArrayOfObject.length > 0))
      this.mListener.onProgressUpdate(this, paramArrayOfObject[0]);
  }

  public void setCancelable(boolean paramBoolean)
  {
    this.isCancelable = paramBoolean;
  }

  public void setListener(TaskListener paramTaskListener)
  {
    this.mListener = paramTaskListener;
  }

  public void update(Observable paramObservable, Object paramObject)
  {
    if ((TaskManager.CANCEL_ALL == (Integer)paramObject) && (this.isCancelable) && (getStatus() == AsyncTask.Status.RUNNING))
      cancel(true);
  }
}